%% ----------------function fill the map----------------------------
function re_map = fillmap(map, M, N)
    re_map = map;
    obs = find(map==9);
    
    for i = 1: numel(obs)
        x = obs(i);
        if x ==1                                    % bottom left
            if map(x+M+1) == 9
                re_map(x+M)=9;
            end
        elseif x==M                                 % top left
            if map(x+M-1) == 9
                re_map(x+M)=9;
            end
        elseif x==(N-1)*M + 1                       % bottom right
            if map(x-M+1) == 9
                re_map(x-M)=9;
            end
        elseif x== M*N                              % top right
            if map(x-M-1) == 9
                re_map(x-M)=9;
            end
        elseif floor(x/M) ==0                       % first row
            if (map(x+M-1) == 9) || (map(x+M+1) == 9)
                re_map(x+M) = 9;
            end
        elseif (floor(x/M) ==N - 1)                 % last row
            if (map(x-M-1) == 9) || (map(x-M+1) == 9)
                re_map(x-M) = 9;
            end
        elseif (mod(x,M) ==1)                       % bottom line
            if (map(x-M+1) == 9) || (map(x+M+1) == 9)
                re_map(x+1) = 9;
            end
        elseif mod(x,M) == 0                        % top line
            if (map(x-M-1) == 9) || (map(x+M-1) == 9)
                re_map(x-1) = 9;
            end
        else                                        % whatever else
            if (map(x-M+1) == 9) || (map(x+M+1) == 9)
                re_map(x+1) = 9;
            end
            if ((map(x-M-1) == 9) || (map(x+M-1) == 9))
                re_map(x-1) = 9;
            end
        end
    end
end